package mscs.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


public class FileDataStoreEx {
	Class readClass;
	File file;
	public FileDataStoreEx(Class readClass, File file) {
		this.readClass = readClass;
		this.file = file;
	}
	@SuppressWarnings({ "unchecked", "finally" })
	public List getObjects() throws Exception {
		ArrayList<Object> list = new ArrayList<Object>();
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(file));
			list = (ArrayList<Object>) ois.readObject();
		} catch (Exception e){
				throw e;
		} finally {
			ois.close();
			return list;
		}
	}
	public void update(List list) throws Exception {
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(file));
			oos.writeObject(list);
		} catch (Exception e) {
			throw e;
		} finally {
			oos.close();
		}
	}
	public void add(List list) throws Exception {
		List newList = getObjects();
		newList.add(list);
		update(newList);
	}
}
